<?php if (!defined('THINK_PATH')) exit();?><!DOCTYPE HTML>
<html class="">
<head>
	
	<meta name="author" content="蓝凌科技">
	<meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="initial-scale=1, maximum-scale=1">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
	<meta name="format-detection" content="telephone=no">
	<meta http-equiv="Cache-Control" content="no-cache, no-store, must-revalidate" />
	<meta http-equiv="Pragma" content="no-cache" />
	<meta http-equiv="Expires" content="0" />

	<link rel="stylesheet" type="text/css" href="/Public/Api/css/sm.min.css">	
	<link rel="stylesheet" type="text/css" href="/Public/Api/css/sm-extend.min.css">	
	
	<title>帮工小哥</title>
	
	<script type="text/javascript">
	    var _root = '<?php echo ($_site_url); echo U(' / ');?>', _controller = '<?php echo ($_controller); ?>';
	</script>
	
	
	<link rel="stylesheet" type="text/css" href="/Public/Api/css/order.css">


</head>
<body>
	
	

<div class="page-group">
    <div class="page page-current">
        <div class="content my-order-coupon">
            <!--优惠券1-->
            <div class="exchange-top">
                <input type="text" name="code" id="code" placeholder="请输入优惠码">
                <a href="#" class="right" onclick="exchange()">兑换</a>
            </div>
            <div class="buttons-tab">
                <a href="#tab_1" onclick="changeTab(this);" class="tab-link button active">可使用</a>
                <a href="#tab_2" onclick="changeTab(this);" class="tab-link button">已使用</a>
                <input type="hidden" name="lastIndex" id="lastIndex" value="0"/>
                <input type="hidden" name="tabIndex" id="tabIndex" value="<?php echo ($tabIndex); ?>"/>
            </div>
            <div class="tabs">
                <div id="tab_1" class="tab active infinite-scroll">
                    <div class="list-block media-list fff">
                        <ul>
                        	
                        </ul>
                    </div>
                    <div class="infinite-scroll-preloader">
						<div class="preloader">
                      	</div>
                    </div>
                </div>
                
                <div id="tab_2" class="tab infinite-scroll">
                    <div class="list-block media-list fff">
                        <ul>
                        
                        
                        </ul>
                    </div>
                    <div class="infinite-scroll-preloader">
						<div class="preloader">
                      	</div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>




	<!-- 底部
================================================== -->
<footer>
</footer>
<script type="text/javascript">
    (function(){
        var ThinkPHP = window.Think = {
            "ROOT"   : "", //当前网站地址
            "APP"    : "/api.php?s=", //当前项目地址
            "PUBLIC" : "/Public", //项目公共目录地址
            "DEEP"   : "<?php echo C('URL_PATHINFO_DEPR');?>", //PATHINFO分割符
            "MODEL"  : ["<?php echo C('URL_MODEL');?>", "<?php echo C('URL_CASE_INSENSITIVE');?>", "<?php echo C('URL_HTML_SUFFIX');?>"],
            "VAR"    : ["<?php echo C('VAR_MODULE');?>", "<?php echo C('VAR_CONTROLLER');?>", "<?php echo C('VAR_ACTION');?>"]
        }
    })();
    
</script>
<script src="/Public/Api/js/zepto.min.js" type="text/javascript">
</script>
<script type="text/javascript">
//坑死人的配置项
//若不加此配置，则所有页面均使用 js 和 css 缓存
$.config = {
   	router: false
}
</script>
<script src="/Public/Api/js/sm.min.js" type="text/javascript">
</script>
<script src="/Public/Api/js/sm-extend.min.js" type="text/javascript">
</script>
<script src="/Public/Api/js/think.js" type="text/javascript">
</script>


<script type="text/javascript">

var target = Think.U('UserCoupon/ajax_couponList');
var params = ['lastIndex', 'tabIndex'];
var container = '.infinite-scroll.active .fff ul';

$("#lastIndex").val('0');

var tb = $("#tabIndex").val();
tb = tb ? tb : '1';
$("a[class*='tab-link']").removeClass('active');
$("a[class*='tab-link']").eq(tb-1).addClass('active');

function changeTab(obj){
	
	var tabHref = $(obj).attr('href');
	var index = tabHref.indexOf('_');
	var tabIndex = tabHref.substr(index+1);

	if(!isNaN(tabIndex)){
		
		$("#tabIndex").val(tabIndex);
		
		if($("#tab_" + tabIndex).find('.infinite-scroll-preloader').css("display")==='block'){
			
			var itemLength = $("#tab_" + tabIndex).find('li').length;
			
			$("#lastIndex").val(itemLength);
			$.init();
			
			if(itemLength == 0){
				addItems();
			}
		}
	}
}

function joinHTML(data){
	
	var html = '';

	if($.isArray(data) && data.length > 0){
		
		var idStr = $('.infinite-scroll.active').attr('id');
		var tabIndex = '1';
		
		if(idStr){
			var index = idStr.indexOf('_');
			tabIndex = idStr.substr(index+1);
		}
		
		$.each(data, function(key, val){
			
			var color = '';
			var type = '';
			var code = '';
			var category = '<span class="activity">活动</span>';
			
			if(tabIndex == 2){
				color = ' coupon-dis';
			}
			
			if(val['minimum'] > 0){
				type = '<li><i class="list-disc"></i>满' + val['minimum'] + '元可用</li>';
			}
			
			if(val['category'] == 1){
				category = '<span class="first-use">首次</span>';
			}else if (val['category'] == 2){
				code = '<li><i class="list-disc"></i>优惠码' + $.trim(val['code']) + '</li>';
				category = '<span class="invite">邀请</span>';
			}
			
			html += '<li><div class="coupon' + color + '">';
			html += '<div class="coupon-text"><p>帮工小哥</p><div class="coupon-detail clearfix">';
			html += '<span class="left coupon-money">￥' + val['money'] + '</span><div class="coupon-use right">';
			html += '<p class="coupon-date">' + formatDate(val['create_time']) + '--' + formatDate(val['deadline']);
			html += '</p></div></div></div><div class="condition"><ul>' + type + code + '</ul></div>';
			html += '<div class="mark">' + category + '</div></div></li>';
			
		});
	}else{
		
		html = '<div class="content-block place-order"><p>暂无可用优惠券</p></div>';
	}

	return html;
}

function exchange(){
	
	var code = $("#code").val();
	if(!(/^\d{8}$/.test(code))){
		$.toast('优惠码格式不正确，请确认');
		return false;
	}
	
	var target = Think.U('UserCoupon/exchange');
	
	$.post(target, {'code':code}, function(data){
		
		if(data.status == '-201'){
			$.alert('身份认证已失效，请重新登录', function () {
		        window.location.href = Think.U('Public/login');
		    });
		}else if(data.status == '422'){
			$.alert('参数异常');
		}else if(data.status == '0'){
			$.alert('优惠码不存在');
		}else if(data.status == '2'){
			$.alert('优惠券已被使用，不可兑换');
		}else if(data.status == '3'){
			$.alert('优惠券已过期，不可兑换');
		}else if(data.status == '-1'){
			$.alert('优惠券所有者为本人，不可兑换');
		}else if(data.status == '1'){
			$.alert('兑换成功', function(){
				window.location.reload();
			});
		}
		
	});
}

</script>
<script src="/Public/Api/js/pageLoading.js" type="text/javascript"></script>

<!-- 用于加载js代码 -->

</body>
</html>